<template>
  <div
    :id="id"
    :class="className"
    :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
import resize from '@/components/Charts/mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    id: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '200px'
    },
    height: {
      type: String,
      default: '200px'
    },
    data: Object
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    data: {
      handler(val) {
        if (val.detect) {
          this.$nextTick(() => {
            this.initChart()
          })
        }
      },
      deep: true
    }
  },
  mounted() {
    this.initChart()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(document.getElementById(this.id))
      const options = {
        color: ['#02c3c3', '#b0eeef'],
        tooltip: {
          trigger: 'item'
        },
        title: {
          text: '{a|' + 20.01 + '}{c|%}',
          x: 'center',
          y: 'center',
          textStyle: {
            rich: {
              a: {
                fontSize: 12,
                color: '#02c3c3'
              },

              c: {
                fontSize: 12,
                color: '#02c3c3'
                // padding: [5,0]
              }
            }
          }
        },
        series: [
          {
            name: '访问来源',
            type: 'pie',
            radius: ['60%', '90%'],
            avoidLabelOverlap: false,
            label: {
              show: false,
              position: 'center',
              fontSize: '40',
              fontWeight: 'bold'
            },
            labelLine: {
              show: false
            },
            data: [
              { value: 1000, name: '19%' },
              { value: 200 }
            ]
          }
        ]
      }
      this.chart.setOption(options)
    }
  }
}
</script>
